package com.delu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.StorageTable;
import com.delu.entity.StorageVO;
import com.delu.entity.StorageVip;
import com.delu.entity.StorageVipVO;
import com.delu.mapper.StorageVipMapper;
import com.delu.utils.IdWorker;
import com.delu.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员卡表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-18
 */
@RestController
@Api(value = "会员卡服务接口")
@RequestMapping("/storageVipController")
@RefreshScope //开启自动刷新配置
public class StorageVipController {

    @Autowired
    private StorageVipMapper storageVipMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 会员卡生成
     */

    @PostMapping
    @ApiOperation("会员卡增加方法")
    //paramType:body 表示@RequestBody接受的实体类 path:表示路径/{} 传参  query @RequestParam接收参数
    @ApiImplicitParam(name = "storageTable",value = "会员卡实体类",required = true,paramType = "body")
    public Result insertStorageTableList(/*@RequestBody StorageTable storageTable*/){
        try {

                List<String> list = RandomUtil.getUserIds(new ArrayList(), 1);
                for (String s : list) {
                    StorageVip storageVip = new StorageVip();
                    System.out.println(s);
                    long nextId = idWorker.nextId();
                    System.out.println(nextId);
                    storageVip.setStorageVipId(String.valueOf(nextId)).setStorageVipPassword(String.valueOf(s)).setStorageVipMoney(new BigDecimal(1000)).setStorageVipStatus(1).setUserId(1234567L);
                    storageVipMapper.insert(storageVip);
                }

            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /**
     * 储值卡删除(逻辑删除)
     * 储值卡不可充值
     */
    @DeleteMapping("/{storageVipId}")
    @ApiOperation(value = "根据会员卡id删除")
    @ApiImplicitParam(name = "storageVipId",value = "会员卡id",required = true,paramType = "path")
    public Result deleteStorageTableById(@PathVariable(value = "storageVipId",required = true) String storageVipId){
        try {
            StorageVip storageVip = storageVipMapper.selectById(storageVipId);
            storageVip.setStorageVipStatus(0);
            storageVipMapper.updateById(storageVip);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @GetMapping
    @ApiOperation(value = "根据会员卡id查询")
    @ApiImplicitParam(name = "storageVipId",value = "会员卡id",required = true,paramType = "query")
    public Result findOrderById(@RequestParam("storageVipId") String storageVipId) {

        try {
            StorageVip storageVip = storageVipMapper.selectById(storageVipId);
            return Result.response(ResultEnum.SUCCESS).setData(storageVip);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有储值卡")
    public Result selectStorageTableList(){
        try {
            List<StorageVip> storageVips = storageVipMapper.selectList(null);
            return Result.response(ResultEnum.SUCCESS).setData(storageVips);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }



    /**
     * 会员卡消费
     *
     */
    @PutMapping("paymentStorageVip")
    @ApiOperation(value = "会员卡消费")
    @ApiImplicitParam(name = "storageVip",value = "会员卡与金额实体VO类",required = true,paramType = "body")
    public Result paymentStorageTable(@RequestBody StorageVipVO storageVipVO){
        try {
            StorageVipVO storageVO1 = JSONObject.parseObject(JSON.toJSONString(storageVipVO), StorageVipVO.class);
            StorageVip storageVip = storageVO1.getStorageVip();
            BigDecimal rentPrice = storageVO1.getRentPrice();
            Long userId = storageVO1.getUserId();
            //1.通过用户输入的卡号卡密来进行查询会员卡余额  商品金额
            StorageVip storageVip1 = storageVipMapper.selectById(storageVip.getStorageVipId());
            //判断卡号是否可用
            if(storageVip1.getStorageVipStatus() != 1){
                return Result.response(ResultEnum.NOT_VIP_CARD);
            }
            //判断卡号是否正确
            if (storageVip1 == null || "".equals(storageVip)){
                return Result.response(ResultEnum.BALANCE).setData(null);
            }
            //判断密码
            if(!storageVip.getStorageVipPassword().equals(storageVip1.getStorageVipPassword())){
                System.out.println(storageVip.getStorageVipPassword());
                System.out.println(storageVip1.getStorageVipPassword());
                return Result.response(ResultEnum.ERRORPWD).setData(null);
            }
            //判断用户id是否正确
            if (!userId.equals(storageVip.getUserId())){
                return Result.response(ResultEnum.NOT_YOU_CAED).setData(null);
            }
            //判断余额是否充足
            if(rentPrice.compareTo(storageVip1.getStorageVipMoney()) < 1){//前端传过来的金额  小于等于 表中金额
                //执行减金额操作
                BigDecimal money = storageVip1.getStorageVipMoney().subtract(rentPrice);
                if (money.compareTo(new BigDecimal(0)) == 0){//如果减完后金额为0的话  修改状态
                    storageVip1.setStorageVipStatus(0).setStorageVipMoney(new BigDecimal(0));//无效
                    storageVipMapper.updateById(storageVip1);

                    //执行添加消费记录操作



                    return Result.response(ResultEnum.PAYMENT_OK).setData("null");
                }
                storageVip1.setStorageVipMoney(money);
                storageVipMapper.updateById(storageVip1);
                return Result.response(ResultEnum.PAYMENT_OK).setData("null");

            }

            return Result.response(ResultEnum.BALANCE).setData("null");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
